package com.db.controller;

import com.db.entity.Person;
import com.db.service.PersonService;
import com.db.service.impl.PersonServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/showPersonController")
public class ShowPersonController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("login");
        Integer userid = (Integer) session.getAttribute("userid");
        if( username == null){
            response.sendRedirect(request.getContextPath()+"/login.jsp");
            return;
        }

        Integer personId = Integer.valueOf(request.getParameter("personsId"));
        PersonService ps = new PersonServiceImpl();
        Person person = ps.showPerson(personId,userid);


        request.setAttribute("Persons",person);
        request.getRequestDispatcher("/updatePerson.jsp").forward(request,response);


    }
}
